package cn.wjssl.domain.strategy.service.rule.tree.impl;

import cn.wjssl.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import cn.wjssl.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.wjssl.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import cn.wjssl.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 保底奖品节点
 */
@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {

    @Override
    public DefaultTreeFactory.TreeActionEntity logicTree(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} ruleValue:{}", userId, strategyId, awardId, ruleValue);

        // 先判断规则树的配置是否正确
        String[] split = ruleValue.split(Constants.COLON);
        if (split.length == 0) {
            log.error("规则过滤-兜底奖品，兜底奖品未配置告警 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
            throw new RuntimeException("兜底奖品未配置 " + ruleValue);
        }

        // 兜底奖品id
        Integer lockawardId = Integer.parseInt(split[0]);
        // 兜底奖品对应的规则, 如果有的话, 没有就是空字符串 ""
        String lockAwardRuleValue = split[1].length() > 0 ? split[1] : "";
        // 已经是叶子节点, 所以拦截
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                        .awardId(lockawardId)
                        .awardRuleValue(lockAwardRuleValue).build())
                .build();
    }
}
